/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

/**
 *
 */
package com.wallellen.wechat.token.server;

import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 * @date 2015年1月29日
 */
public abstract class AbsServer implements IServer {

    private static Logger logger = LoggerFactory.getLogger(AbsServer.class);

    protected String customerServerClass;

    public AbsServer() {
        this.customerServerClass = getCustomerServerClass();
    }

    @Override
    public String token() {
        return server().token();
    }

    /**
     * 得到系统可用的中控服务器
     *
     * @return 正在使用的中控服务器
     */
    public IServer server() {
        if (isCustomer())
            return customerServer();
        return defaultServer();
    }

    /**
     * 加载自定义中控服务器
     *
     * @return 自定义的中控服务器
     */
    public IServer customerServer() {
        String className = customerServerClass;
        IServer customerServer = null;
        try {
            Class clazz = Class.forName(className);
            customerServer = (IServer) clazz.newInstance();
        } catch (Exception e) {
            logger.error("系统找不到" + className);
            logger.error("自定义server实例化失败，" + e.getMessage());
            e.printStackTrace();
        }
        return customerServer;
    }

    /**
     * 如果配置文件中配置了AccessTokenServer，那么使用客户自定义server
     *
     * @return 是否配置了自定义中控服务器
     */
    public boolean isCustomer() {
        return !Strings.isNullOrEmpty(customerServerClass);
    }

    /**
     * 指定的默认中控服务器
     *
     * @return 默认的中控服务器
     */
    public abstract IServer defaultServer();

    /**
     * 自定义服务器的类
     *
     * @return
     */
    protected abstract String getCustomerServerClass();
}
